<?php
use Mirage\Web;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Report
 *
 * @author bry
 */
class TaskController extends BaseReportController{
    public function actionOverview() {
        if($this->detect->isMobile()) {
            $data = TaskModel::model()->findAll();
        } else {
            if(Web::app()->isAjaxRequest()) {
                echo TaskModel::model()->findAll();
                Web::app()->end();
            } else {
                $this->view->render('task/task_overview');
            }
        }
    }
    
    public function actionAdd() {
        $form = new TaskForm();
        $form->setModel(TaskModel::model());
        if(\Mirage\Web::app()->isPostRequest()) {
            TaskModel::model()->setAttribute($_POST['TaskModel']);
            TaskModel::model()->save();
            Web::app()->redirect($this->createUrl('view',array('id'=>  TaskModel::model()->id)));
        }
        $this->view->render('task/task_add', array('form'=>$form));
    }
    
    public function actionEdit() {
        $data = TaskModel::model()->findByID($_GET['id']);
        TaskModel::model()->setAttribute($data);
        if(Web::app()->isAjaxRequest()) {
            if(Web::app()->isPostRequest()) {
                TaskModel::model()->setAttribute($_POST['TaskModel']);
                if(TaskModel::model()->updateByID($_GET['id'])) {
                    echo json_encode(array('success'=>true));
                    Web::app()->end();
                }
            }
            $this->view->renderPartial('task/task_update');
        } else {
            $this->view->render('task/task_update');
        }
    }
    
    public function actionView() {
        $data = TaskModel::model()->findByID($_GET['id']);
        if(Web::app()->isAjaxRequest()) {
            $this->view->renderPartial('task/task_view', array('data'=>$data));
        }else {
            $this->view->render('task/task_view', array('data'=>$data));        
        }
        
    }
    
    public function actionSave() {
        
    }
    
    public function actionDelete() {
        if(TaskModel::model()->deleteByID($_GET['id'])) {
            Web::app()->setFlash('result', 'Task id ' . $_GET['id'] . ' is successfully deleted');
        } 
        Web::app()->redirect($this->createUrl('overview'));
    }
    
}

